package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Article;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.ArticleDetailVo;
import com.sangeng.domain.vo.ArticleListVo;
import com.sangeng.domain.vo.HotArticleVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.ArticleMapper;
import com.sangeng.service.ArticleService;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @auther qihs
 * @Date
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    @Lazy //处理循环注入
    private CategoryService categoryService;

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章，封装成ResponseResult
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件：正式文章;已经配置过逻辑删除，不用处理已删除的文章
        //此处参数“方法引用”:推荐使用方法引用
        //如果使用QueryWrapper，可以写“列名”，
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //查前10条；
        Page<Article> page = new Page<>(SystemConstants.PAGE_TOP, SystemConstants.PAGE_SARTICLE);
        //传入查询条件
        page(page,queryWrapper);
        List<Article> articles = page.getRecords();
/**   改用工具类进行 拷贝
 *   List<HotArticleVo> articleVos =new ArrayList<>();
 *  for (Article article : articles) {
            HotArticleVo vo = new HotArticleVo();
            //bean拷贝，将返回数据articles,封装为vo
            BeanUtils.copyProperties(article, vo);
            articleVos.add(vo);
        }*/
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {

        /**查询条件
         *  1、如果有categoryid，查询时要和传入的相同；如果没有不需要查询
         *  2、状态是已经发布的文章，按照isTop进行排序，置顶文章在前
         */
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询文章状态已发布,并使用三参形式，进行判断：categoryId不为空且>0
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId) && categoryId>0,
                                        Article::getCategoryId,
                                         categoryId);
        // 状态是正式发布的 ___注：忘记一次了
        lambdaQueryWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        /**两参数形式，与上面查询效果相同，但是需要额外进行判断
         * if（Objects.nonNull(categoryId) && categoryId>0）{
         * lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);}
         * */
        //isTop 降序排序
        lambdaQueryWrapper.orderByDesc(Article::getIsTop);

        //分页查询,拿到分页对象，传入分页参数
        Page<Article> page = new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);

        //查询categoryName
        List<Article> articles = page.getRecords();

        //categoryId去查询categoryName 进行设置
        articles.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());
//        for (Article article : articles) {
//            Category category = categoryService.getById(article.getCategoryId());
//            article.setCategoryName(category.getName());
//        }
        //封装查询结果
        List<ArticleListVo> articlesListVos = BeanCopyUtils.copyBeanList(page.getRecords(), ArticleListVo.class);
        /**     page.getRecords());//获取分页记录
                page.getPages();//总页数
                page.getTotal();//总记录数
                page.hasNext();//是否有下一页
                page.hasPrevious();//是否有上一页*/
        //将查询结果与Total一起封装为pageVo
        PageVo pageVo = new PageVo(articlesListVos, page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {

        //根据id查询文章
        Article article = getById(id);
        //转换成VO
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        //根据分类id查询分类名
        Long categoryId = articleDetailVo.getCategoryId();
        Category category = categoryService.getById(categoryId);
        if (category != null){
            articleDetailVo.setCategoryName(category.getName());
        }
        //封装响应，返回数据
        return ResponseResult.okResult(articleDetailVo);
    }


}
